﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsProvider.cs" company="DevDigital">
//   DevDigital
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Settings.NET
{
    using System;
    using System.Linq.Expressions;

    /// <summary>
    /// Settings provider
    /// </summary>
    [Serializable]
    internal class SettingsProvider : ISettingsProvider
    {
        /// <summary>
        /// The settings provider factory
        /// </summary>
        private readonly SettingsProviderFactory settingsProviderFactory;

        /// <summary>
        /// The settings storage
        /// </summary>
        private readonly ISettingsStorage settingsStorage;

        /// <summary>
        /// The settings serializer
        /// </summary>
        private readonly ISettingsSerializer settingsSerializer;

        /// <summary>
        /// The settings cache
        /// </summary>
        private readonly ISettingsCache settingsCache;

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsProvider"/> class.
        /// </summary>
        /// <param name="settingsProviderFactory">The settings provider factory.</param>
        /// <param name="settingsStorage">The settings storage.</param>
        /// <param name="settingsSerializer">The settings serializer.</param>
        /// <param name="settingsCache">The settings cache.</param>
        internal SettingsProvider(            
            SettingsProviderFactory settingsProviderFactory,
            ISettingsStorage settingsStorage,
            ISettingsSerializer settingsSerializer = null,
            ISettingsCache settingsCache = null)
        {
            this.settingsProviderFactory = settingsProviderFactory;
            this.settingsStorage = settingsStorage;
            this.settingsSerializer = settingsSerializer;
            this.settingsCache = settingsCache;
        }

        /// <summary>
        /// Gets a settings configuration by key
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// The settings configuration
        /// </returns>
        public ISettingConfiguration Key(string key)
        {
            var setting = new SettingConfiguration(
                key,
                this.settingsProviderFactory,
                this.settingsStorage,
                this.settingsSerializer, 
                this.settingsCache);
            
            return setting;
        }

        /// <summary>
        /// Gets a settings configuration by key
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="property">The property.</param>
        /// <returns>
        /// The settings configuration
        /// </returns>
        public ISettingConfiguration Key<TProperty>(Expression<Func<TProperty>> property)
        {
            var keyName = ConversionHelper.PropertyName(property);
            return this.Key(keyName);
        }

        public ISettings Keys<TValue>(Keys<TValue> keys)
        {
            throw new NotImplementedException();
        }
    }
}